package entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * @author lanhai
 * @param <T>
 */
@ApiModel(description = "Result", value = "Result")
public class Result<T> implements Serializable {

    @ApiModelProperty(value = "执行是否成功[true:成功,false:失败]", required = true)
    private boolean flag;//是否成功
    @ApiModelProperty(value = "返回状态码[200:成功,201:失败,202:用户名或密码错误,203:权限不足,204:远程调用失败,205:重复操作,206:没有对应的抢购数据]", required = true)
    private Integer code;//返回码
    @ApiModelProperty(value = "返回消息", required = true)
    private String message;//返回消息
    @ApiModelProperty(value = "返回数据", required = true)
    private T data;//返回数据

    private Result() {}

    public Result(boolean flag, Integer code, String message, T data) {
        this.flag = flag;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Result(boolean flag, Integer code, String message) {
        this.flag = flag;
        this.code = code;
        this.message = message;
    }

    public static <T>Result<T> def(){
        return new Result<T>();
    }

    public static <T>Result<T> ok() {
        return ok(StatusCode.MSG_SUCCESS);
    }

    public static <T>Result<T> ok(String msg) {
        return new Result(true, StatusCode.OK, msg, null);
    }

    public static <T>Result<T> data(T data) {
        return new Result<T>(true, StatusCode.OK, StatusCode.MSG_SUCCESS, data);
    }

    public static <T>Result<T> error() {
        return error(StatusCode.MSG_FAILED);
    }

    public static <T>Result<T> error(String msg) {
        return new Result(false, StatusCode.ERROR, msg, null);
    }


    public boolean isFlag() {
        return flag;
    }

    public Result<T> setFlag(boolean flag) {
        this.flag = flag;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public Result<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public Result<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }
}
